import CombatBase from '@/Battle/CombatBase.js';

export class CombatUnit extends CombatBase{
    constructor(battler){
        super();
        this.battler=battler;
        this.hp=0;
        this.mp=0;
        this.tp=0;

        this.agi=0;

        //rpgmakermv的状态与此处buff等效，不想整得太复杂
        this.buffs=[];
    }

    actions(){
        return this.battler?._actions;
    }

    action(index){
        return this.battler?.action(index);
    }

    makeActions(){
        this.battler?.makeActions();
    }

    shiftAction(){
        return this.battler?.action.shift();
    }

    unshiftAction(...actions){
        this.battler?._actions.unshift(...actions);
    }

    requestAddBuff(buff){
        if(buff){
            buff.requestAdd(this,null,()=>{
                this.forceAddBuff(buff);
            });
        }
    }
    forceAddBuff(buff){
        let index=this.buffs.findIndex(bf=>bf.id==buff.id);
        if(index<0){
            this.buffs.push(buff);
            index=this.buffs.length-1;
        }
        this.buffs[index].onAdded();
    }

    requestRemoveBuff(buff){
        let index=this.buffs.findIndex(bf=>bf.id==buff.id);
        if(index>=0){
            buff.requestRemove(this,null,()=>{
                this.forceRemoveBuffByIndex(index);
            });
        }
    }

    forceRmoveBuff(buff){
        let index=this.buffs.findIndex(bf=>bf.id==buff.id);
        if(index>=0){
            this.buffs.splice(index,1);
            buff.onRemoved();
        }
    }
    forceRemoveBuffByIndex(index){
        if(index<this.buffs.length){
            let buff=this.buffs[index];
            this.buffs.splice(index,1);
            buff.onRemoved();
        }
    }
}